#include<linux/module.h>
#include <linux/init.h>
#include<linux/slab.h>
#include<linux/kernel.h>
#include<linux/input.h>
#include <linux/fs.h>
#include <linux/dbg_tool.h>
#include "input_debug.h"


static struct input_dev *vir_input_dev;
//static struct class *vir_class;
static ssize_t show_name(struct device *pdev, struct device_attribute *attr, char *buf)
{
	ker_err("\n");
	input_report_key(vir_input_dev, KEY_POWER,  1);
	input_report_key(vir_input_dev, KEY_POWER,  0);
	input_sync(vir_input_dev);
	return 0;
}

static ssize_t store_name(struct device *pdev, struct device_attribute *attr,
					const char *buf, size_t size)
{
	ker_err("\n");
	input_report_key(vir_input_dev, KEY_HOME,  1);
	input_report_key(vir_input_dev, KEY_HOME,  0);
	input_sync(vir_input_dev);
	return size;
}

DEVICE_ATTR(vir_event, S_IWUGO | S_IRUGO, show_name, store_name);

void	remove_input(struct input_dev *dev)
{
	input_unregister_device(dev);
	input_free_device(dev);
}

struct input_dev *register_input(char *name, u8 input_arr)
{
	struct input_dev *dev;
	int ret;
	dev = input_allocate_device();
	if(!dev)
		goto fail1;


	ret = input_register_device(dev);
	if(ret)
	  goto fail2;

	return dev;
fail2:
	input_free_device(dev);
fail1:
	return NULL;
}

int input_dbg_entry(void)
{
#if 1
	amtel_init();
	return 0;
#else 
	int ret;
	vir_input_dev = input_allocate_device();
	if(!vir_input_dev)
	{
		printk("zoucao input device allocation failed\n");
		return 1;
	}

	vir_input_dev->name = "vir_input";
	ret = input_register_device(vir_input_dev);
	if(ret)
	{
		printk("zoucao input register device failed\n");
		goto fail1;
	}
	/*set device attr*/
	__set_bit(EV_KEY, vir_input_dev->keybit);
	__set_bit(KEY_HOME, vir_input_dev->keybit);
	__set_bit(KEY_MENU, vir_input_dev->keybit);
	__set_bit(KEY_BACK, vir_input_dev->keybit);
	__set_bit(KEY_SEARCH, vir_input_dev->keybit);
	__set_bit(KEY_POWER, vir_input_dev->keybit);

	//vir_class = class_create(THIS_MODULE, "vir_input");
	//if (!vir_class) {
	//	printk("create class failed\n");
	//	goto fail2;
	//}
	ret = device_create_file(&vir_input_dev->dev, &dev_attr_vir_event);
	if (ret) {
		printk("create device file failed\n");
	}

	/*atmel driver initail*/

	return 0;
fail1:
	input_free_device(vir_input_dev);
	vir_input_dev = NULL;

	return 1;
#endif
}

void input_dbg_exit(void)
{
#if 0 
	if(vir_input_dev)
	{
		device_remove_file(&vir_input_dev->dev, &dev_attr_vir_event);
		input_unregister_device(vir_input_dev);
		input_free_device(vir_input_dev);
		vir_input_dev = NULL;
	}
#else
	amtel_exit();
#endif
	//class_destroy(vir_class);
}

module_init(input_dbg_entry);
module_exit(input_dbg_exit);
MODULE_AUTHOR("Iiro Valkonen");
MODULE_DESCRIPTION("Driver for Atmel maXTouch Touchscreen Controller");
MODULE_LICENSE("GPL");
